package org.jeecg.modules.database.sharding.entity;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.modules.database.sharding.constats.LogConstant;
import org.jeecg.modules.database.sharding.utils.SpringContextUtil;
import org.jeecgframework.poi.excel.annotation.Excel;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * <p>
 * 系统日志表
 * </p>
 */
@Data
@TableName("api_log")
public class ShardingApiLog implements Serializable {
    private static final long serialVersionUID = 1L;

    /**主键*/
    @TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "主键")
    private java.lang.String id;
    /**请求接口名称*/
    @Excel(name = "请求接口名称", width = 15)
    @ApiModelProperty(value = "请求接口名称")
    private java.lang.String apiName;

    private String apiNameEn;

    /**所属业务系统*/
    @Excel(name = "所属业务系统", width = 15)
    @ApiModelProperty(value = "所属业务系统")
    private java.lang.String belongService;

    /**所属业务系统中文名*/
    private java.lang.String belongServiceName;

    /**客户端名称*/
    @Excel(name = "客户端名称", width = 15)
    @ApiModelProperty(value = "客户端名称")
    private java.lang.String clientName;

    private String clientId;

    //菜单名称---三方接口提供
    private String menuName;
    //请求地址
    //private String url;

    private String params;

    private String requestBody;

    //操作日志接口字段
    private String operationName;

    private String operationType;

    private Object oldRecord;

    private Object newRecord;

    private String methodType;

    private String requestUrl;

    private String isServiceLog;

    /**用户身份证号码*/
    @Excel(name = "用户身份证号码", width = 15)
    @ApiModelProperty(value = "用户身份证号码")
    private String userIdCard;

    //日志类型（1.登录日志，2.中台日志，3.访问日志，4.操作日志）
    private String logType;

    /**返回状态*/
    @Excel(name = "返回状态", width = 15)
    @ApiModelProperty(value = "返回状态")
    private java.lang.String respStatus;

    /**响应时间*/
    @Excel(name = "响应时间", width = 15)
    @ApiModelProperty(value = "响应时间")
    private Long respTime;

    /**日志详情*/
    @Excel(name = "日志详情", width = 15)
    @ApiModelProperty(value = "日志详情")
    private java.lang.String logDesc;

    /**创建人*/
    @ApiModelProperty(value = "创建人")
    private java.lang.String createBy;

    /**创建日期*/
    @ApiModelProperty(value = "创建日期")
    private java.util.Date createTime;

    /**更新人*/
    @ApiModelProperty(value = "更新人")
    private java.lang.String updateBy;

    /**更新日期*/
    @ApiModelProperty(value = "更新日期")
    private java.util.Date updateTime;




    public ShardingApiLog(long cost, Integer code, Object result, String apiName, String apiNameEn, String belongService,
                  String belongServiceName,
                  String clientName, String clientId) {
        this.respTime =cost;
        this.respStatus = String.valueOf(code);
        this.apiName =apiName;
        this.apiNameEn = apiNameEn;
        this.logDesc = JSON.toJSONString(result);
        this.belongService = belongService;
        this.belongServiceName = belongServiceName;
        this.clientName = clientName;
        this.clientId = clientId;
        HttpServletRequest request = SpringContextUtil.getHttpServletRequest();
        this.requestUrl = request.getRequestURI();
        this.methodType = request.getMethod();

        String invokerId = request.getHeader(LogConstant.INVOKER_ID) .isEmpty() ? "" :
                "Invoker-Id:" +request.getHeader(LogConstant.INVOKER_ID);
        String invokerSignature = request.getHeader(LogConstant.INVOKER_SIGNATURE).isEmpty() ? "" :
                "Invoker-Signature:" + request.getHeader(LogConstant.INVOKER_SIGNATURE);
        this.params = invokerId + "," + invokerSignature;

    }

    public ShardingApiLog() {

    }

}
